import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

//解决导航栏或者底部导航tabBar中的vue-router在3.0版本及以上频繁点击菜单报错问题
//获取原型对象上的push函数
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
//修改原型对象中的push方法，replace方法就把VueRouter.prototype.push的push换成replace即可
  return originalPush.call(this, location).catch(err => err)
}

const routes = [
  //  首页及子页面(公告,新闻消息,用户个人信息修改等)
  {
    path: '/',
    name: 'Manager',
    component: () => import('../views/Manager.vue'),
    redirect: '/home', //重定向到home主页
    children:[
        //主页
      { path: 'home', name: 'Home', meta: { name: '系统首页'}, component: () => import('../views/manager/Home')},
        //用户相关(管理,个人信息,重置密码)
      { path: 'user', name: 'User', meta: { name: '用户信息'}, component: () => import('../views/manager/user/User')},
      { path: 'person', name: 'Person', meta: { name: '个人信息'}, component: () => import('../views/manager/user/Person')},
      { path: 'password', name: 'Password', meta: { name: '修改密码'}, component: () => import('../views/manager/user/Password')},
        //基础配置(竞赛标签,学部类型)
      { path: 'sign', name: 'Sign', meta: { name: '竞赛标签配置'}, component: () => import('../views/manager/baseConfig/Sign')},
      { path: 'faculty', name: 'Faculty', meta: { name: '学部配置'}, component: () => import('../views/manager/baseConfig/Faculty')},
        //竞赛信息
      { path: 'news', name: 'News', meta: { name: '竞赛信息'}, component: () => import('../views/manager/news/News')},
      { path: 'newsDetail', name: 'NewsDetail', meta: { name: '竞赛详情'}, component: () => import('../views/manager/news/NewsDetail')},
        //报名
      { path: 'enroll', name: 'Enroll', meta: { name: '报名信息'}, component: () => import('../views/manager/enroll/Enroll')},
      { path: 'enrollmanage', name: 'EnrollManage', meta: { name: '报名管理'}, component: () => import('../views/manager/enroll/EnrollManage')},
      { path: 'enrollForm', name: 'EnrollForm', meta: { name: '报名单'}, component: () => import('../views/manager/enroll/EnrollForm')},
      { path: 'enrollDetail', name: 'EnrollDetail', meta: { name: '学生报名'}, component: () => import('../views/manager/enroll/EnrollDetail')},
        //成绩
      { path: 'setGrade', name: 'SetGrade', meta: { name: '成绩评分'}, component: () => import('../views/manager/grade/SetGrade')},
      { path: 'grade', name: 'Grade', meta: { name: '成绩查询'}, component: () => import('../views/manager/grade/Grade')},
      { path: 'gradeManage', name: 'GradeManage', meta: { name: '成绩查询'}, component: () => import('../views/manager/grade/GradeManage')},
        //支付信息
      { path: 'payManage', name: 'PayManage', meta: { name: '支付信息'}, component: () => import('../views/manager/pay/PayManage')},
        //公告
      { path: 'notice', name: 'Notice', meta: { name: '系统公告'}, component: () => import('../views/manager/Notice')},
        //日志
      { path: 'logs', name: 'Logs', meta: { name: '系统日志'}, component: () => import('../views/manager/Logs')},

      //  无权访问时跳转的页面 403:无权访问
      {path: '403', name: 'Auth', meta: { name: '无权限'}, component: () => import('../views/manager/Auth.vue')},
    ]
  },
  //  登录
  {path: '/login', name: 'Login', meta: { name: '登录'}, component: () => import('../views/Login.vue')},
  //  注册
  {path: '/register', name: 'Register', meta: { name: '注册'}, component: () => import('../views/Register.vue')},
  //  其他路由
  {path: '*', name: '404', meta: { name: '无法访问'}, component: () => import('../views/404.vue')},

]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

//路由守卫
router.beforeEach((to, from, next) => {
//  to 是到达路由的信息
//  from 是来源的路由信息
//  next 是帮助跳转路由的函数
  let adminPath = ['/user','/sign','/faculty'] //管理员的路由路径
  let user = JSON.parse(localStorage.getItem('honey-user') || '{}')
  if(user .role !== '管理员' && adminPath.includes(to.path) ){
    //如果当前用户不是管理员,当前路径是只有管理员才能访问的路径,让该用户去一个没有权限的页面,不让访问该页面
    next('/403')
  }else {
    next()
  }
})

export default router
